package com.theme.common.office.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @name: Excl个人简历，单元格合并、插入图片等 <tb>
 * @title: https://blog.csdn.net/u012998306/article/details/95063920  <tb>
 * @name: Excel宽度高度、字体强制换行、边框、字体设置等样式控制 <tb>
 * @title: https://blog.csdn.net/u012998306/article/details/95059994  <tb>
 * @author: cuixinfu@51play.com <tb>
 * @date: 2020/9/27 18:04 <tb>
 */
public class ResumeExcelUtil {
    public static void main(String[] args) {
        FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        //先把读进来的图片放到一个ByteArrayOutputStream中，以便产生ByteArray
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            bufferImg = ImageIO.read(new File("E:/测试文件/excel/avatar.jpg"));
            ImageIO.write(bufferImg, "jpg", byteArrayOut);

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet1 = workbook.createSheet("自定义图片");
            // 利用HSSFPatriarch将图片写入EXCEL
            HSSFPatriarch patri_item = sheet1.createDrawingPatriarch();

            //HSSFClientAnchor anchor_item = new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 0, (short) 6, 2);
            HSSFClientAnchor anchor_item = new HSSFClientAnchor(0, 0, 0, 0, (short) 8, 0, (short) 9, 3);
            patri_item.createPicture(anchor_item, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

            HSSFCellStyle style = null; // 建立新的cell样式
            HSSFRow rows1 = sheet1.createRow(0);
            rows1.setHeight((short) 0x300);
            HSSFCell cell10 = rows1.createCell(0);
            cell10.setCellStyle(style);
            cell10.setCellValue("姓名");
            HSSFCell cell11 = rows1.createCell(1);
            cell11.setCellStyle(style);
            HSSFCell cell12 = rows1.createCell(2);
            cell12.setCellStyle(style);
            cell12.setCellValue("性别");
            HSSFCell cell13 = rows1.createCell(3);
            cell13.setCellStyle(style);
            HSSFCell cell14 = rows1.createCell(4);
            cell14.setCellStyle(style);
            cell14.setCellValue("出生年月");
            HSSFCell cell15 = rows1.createCell(5);
            cell15.setCellStyle(style);
            HSSFCell cell16 = rows1.createCell(6);
            cell16.setCellStyle(style);
            cell16.setCellValue("民族");
            HSSFCell cell17 = rows1.createCell(7);
            cell17.setCellStyle(style);
            HSSFCell cell18 = rows1.createCell(8);
            sheet1.addMergedRegion(new CellRangeAddress(0, 2, 8, 8));//合并单元格
            cell18.setCellStyle(style);

            HSSFRow rows12 = sheet1.createRow(12);
            rows12.setHeight((short) 0x250);//设置行高
            HSSFCell cell120 = rows12.createCell(0);
            sheet1.addMergedRegion(new CellRangeAddress(12, 15, 0, 0));
            cell120.setCellStyle(style);
            cell120.setCellValue("家庭成员");

            fileOut = new FileOutputStream("E:\\测试文件\\Excel\\个人简历.xls");

            // 写入excel文件
            workbook.write(fileOut);
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
